
from selenium import  webdriver
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.wait import WebDriverWait
import traceback
from tools.logger import Logger
from datetime import datetime
from selenium.webdriver.support import expected_conditions as EC


class BasePage():


    def __init__(self,driver):
        # self.driver=webdriver.Chrome()
        self.driver=driver
        self.logger=Logger()

    def open(self,url):
        self.driver.get(url)
        self.driver.implicitly_wait(10)
        self.driver.maximize_window()
    def get_element(self,locator,imgname):
        """
        元素定位
        :param locator: 元素定位方式(元组类型)
        :return:
        """
        self.logger.info('开始定位元素%s'%str(locator))
        try:
            start_time=datetime.now()
            self.wait(locator)
            element=self.driver.find_element(*locator)
            end_time=datetime.now()
            times=(end_time-start_time).seconds
            self.logger.info('元素%s定位成功,用时%d秒'%(str(locator),times))
        except Exception as e:
            element=None
            self.logger.error(traceback.format_exc())
            self.logger.info("元素%s定位失败" % str(locator))
            self.get_screen_shoot(imgname)
        return element

    def wait(self,locator,timeout=10,poll=1):
        '''
        输入操作
        :param locator: 定位方式
        :param timeout: 最长等待时间
        :param poll: 间隔时间
        :return:
        '''
        self.logger.info('开始等待元素%s'%str(locator))
        try:
            WebDriverWait(self.driver,timeout,poll).until(EC.visibility_of_element_located(locator))
            self.logger.info('元素%s等待成功'%str(locator))
        except Exception as e:
            self.logger.warning(traceback.format_exc())
            self.logger.info('元素%s等待失败'%str(locator))
            return
    def click(self,locator,imgname):
        """
        点击操作
        :param locator:
        :return:
        """
        element=self.get_element(locator,imgname)
        if element is None:
            self.logger.info('元素%s定位为空,不能进行点击操作,请确认'%str(locator))
            return
        element.click()

    def input(self,locator,text,imgname):
        """
        输入操作
        :param locator: 元素定位方式(元组类型)
        :param text: 输入内容
        :return:
        """
        element=self.get_element(locator,imgname)
        if element is None:
            self.logger.info('元素%s定位为空,不能进行输入操作,请确认'%str(locator))
            return
        element.send_keys(text)


    def clear(self,locator,imgname):
        """
        清空操作
        :param locator:
        :return:
        """
        element = self.get_element(locator,imgname)
        if element is None:
            self.logger.info('元素%s定位为空,不能进行清除操作,请确认'%str(locator))
            return
        element.clear()


    def switch_window(self,index=-1):
        """
        切换浏览器窗口
        :param locator:
        :return:
        """
        haddle=self.driver.window_handles
        self.driver.switch_to.window(haddle[index])

    def switch_into_frame(self,locator,imgname):
        """
        切入frame
        :param locater:
        :return:
        """
        element=self.get_element(locator,imgname)
        if element is None:
            self.logger.info('元素%s定位为空,不能进行frame切入操作,请确认'%str(locator))
            return
        self.driver.switch_to.frame(element)


    def switch_outo_frame(self):
        """
        切出frame
        :return:
        """
        self.driver.switch_to.default_content()

    def switch_alter(self,action='a',text=None):
        """
        切换alter
        :param action:
        :param text:
        :return:
        """
        alter=self.driver.switch_to.alert
        if action=='a':
            alter.accept()
        elif action=='d':
            alter.dismiss()
        elif action=='t':
            return alter.text
        else:
            alter.send_keys(text)
            alter.accept()

    def select(self,locator,imgname,index=None,value=None,visible_text=None):
        """
        下拉框操作
        :param locater: 定位方式
        :param index: 下标
        :param value: value值
        :param visible_text: 可见文本
        :return:
        """
        element=self.get_element(locator,imgname)
        if element is None:
            self.logger.info('元素%s定位为空,不能进行下拉框操作,请确认'%str(locator))
            return
        select=Select(element)
        if index is not None and value is None and visible_text is None:
            select.select_by_index(index)
        if value is not None and index is None and visible_text is None:
            select.select_by_value(value)
        if visible_text is not None and value is None and index is None:
            select.select_by_visible_text(visible_text)

    def execute_js(self,js,element=None):
        '''
        执行JavaScript脚本
        :param js:
        :param element:
        :return:
        '''
        self.driver.execute_script(js,element)

    def get_element_text(self,locator,imgname):
        """
        获取元素标签文本值
        :param locater:
        :return:
        """
        element=self.get_element(locator,imgname)
        if element is None:
            self.logger.info('元素%s定位为空,不能获取元素标签的文本值,请确认'%str(locator))
            return
        text=element.text
        return text

    def get_element_attribute(self,locator,attribute,imgname):
        """
        获取标签元素属性值
        :param locater:
        :param attribute:
        :return:
        """
        element=self.get_element(locator,imgname)
        if element is None:
            self.logger.info('元素%s定位为空,不能获取标签元素属性值,请确认'%str(locator))
            return
        value=element.get_attribute(attribute)
        return value

    def get_screen_shoot(self,imgname):
        """
        截图操作
        :param imgname: 图片名称(模块名_功能名_操作名)
        :return:
        """
        path='./test_results/img/'+imgname+'.png'
        self.driver.get_screenshot_as_file(path)

